#include "settingwidget.h"
#include "ui_settingwidget.h"

#include "mencoder.h"
#include "encoderoption.h"
#include "videoqualitysettingdialog.h"

#include <QStringList>
#include <QButtonGroup>

#define SIZE_BASE_ON_HEIGHT
#define SIZE_BASE_ON_WIDTH
#define SIZE_BASE_ON_

SettingWidget::SettingWidget(EncoderOption *op, Mencoder *c, QWidget *p) :
    ui(new Ui::SettingWidget),
    m_coder(c),
    m_option(op),
    m_lastExtraConfigWidget(NULL)
{
    m_lastSizeInputFocus = NULL;
    ui->setupUi(this);
    loadSupportedCodecs();
    setup();
    setParent(p);
}

SettingWidget::~SettingWidget()
{
    delete ui;
}

void SettingWidget::setup() {
    QButtonGroup *grp;

    grp = new QButtonGroup(this);
    grp->addButton(ui->rb16to9);
    grp->addButton(ui->rb4to3);
    grp->addButton(ui->rbKeepOriginal);
    grp->addButton(ui->rbHeighFixed);
    grp->addButton(ui->rbWidthFixed);
    grp->addButton(ui->rbCustomSize);
    grp->setExclusive(true);
    ui->rbCustomSize->setChecked(true);

    foreach(QAbstractButton *btn, grp->buttons()){
	connect(btn,SIGNAL(clicked()),this,SLOT(reValueSize()));
    }

    grp = new QButtonGroup(this);
    grp->addButton(ui->rbCustomQ);
    grp->addButton(ui->rbHighQ);
    grp->addButton(ui->rbMediumQ);
    grp->addButton(ui->rbLowQ);
    grp->setExclusive(true);

    grp = new QButtonGroup(this);
    grp->addButton(ui->rbCustomAQ);
    grp->addButton(ui->rbHighAQ);
    grp->addButton(ui->rbMediumAQ);
    grp->addButton(ui->rbLowAQ);
    grp->setExclusive(true);

    connect(ui->heightInput,SIGNAL(editingFinished()),
	    this,SLOT(onSizeInputEditingFinished()));

    connect(ui->widthInput,SIGNAL(editingFinished()),
	    this,SLOT(onSizeInputEditingFinished()));

    m_videoQualitySetting = new VideoQualitySettingDialog(m_option,this);

    setExtraConfigDisplay(false);
}


void SettingWidget::syncSettings() {

}

void SettingWidget::loadSupportedCodecs() {
    QList<EncoderCodec *> codecs;
    QVariant data;
    QString name;

    codecs = m_coder->supportedAudioCodecs();

    foreach (EncoderCodec *c, codecs){
	data.setValue(c);
	name = c->name();
	if (c->isRecommended()) name += QObject::tr(" (Recommended)");
	ui->cbAudioCodecs->addItem(name,data);
    }

    codecs = m_coder->supportedVideoCodecs();

    foreach (EncoderCodec *c, codecs){
	data.setValue(c);
	name = c->name();
	if (c->isRecommended()) name += QObject::tr(" (Recommended)");
	ui->cbVideoCodec->addItem(name,data);
    }
}

void SettingWidget::onSizeInputEditingFinished()
{
    QSpinBox *b = qobject_cast<QSpinBox *>(sender());
    if (!b)return;

    m_lastSizeInputFocus = b;

    reValueSize();
}

void SettingWidget::reValueSize( ) {
    QSpinBox *last = m_lastSizeInputFocus;
    if (!last) last = ui->heightInput;


    bool heightInputEnable = true, widthInputEnable = true;

    if (ui->rb16to9->isChecked()) {
	if (last == ui->heightInput) {
	    ui->widthInput->setValue(ui->heightInput->value() * 16 / 9);
	}else{
	    ui->heightInput->setValue(ui->widthInput->value() * 9 / 16);
	}
    }else if (ui->rb4to3->isChecked()) {
	if (last == ui->heightInput) {
	    ui->widthInput->setValue(ui->heightInput->value() * 4 / 3);
	}else{
	    ui->heightInput->setValue(ui->widthInput->value() * 3 / 4);
	}
    }else if (ui->rbHeighFixed->isChecked()) {
	widthInputEnable = false;
    }else if (ui->rbWidthFixed->isChecked()) {
	heightInputEnable = false;
    }else if (ui->rbKeepOriginal->isChecked()) {
	widthInputEnable = false;
	heightInputEnable = false;
    }

    ui->widthInput->setEnabled(widthInputEnable);
    ui->heightInput->setEnabled(heightInputEnable);
}

void SettingWidget::on_cbVideoCodec_currentIndexChanged(int index)
{
    QVariant data = ui->cbVideoCodec->itemData(index,Qt::UserRole);
    EncoderCodec *codec = NULL;
    QString help;
    if (data.isValid()) {
	codec = data.value<EncoderCodec *>();
	help = codec->introduction();
    }

    if (codec) {
	// Setup codec option
	SettingWidgetInterface *common, *advance;

	common = codec->settingWidget(EncoderCodec::PART_COMMON,*m_option);
	advance = codec->settingWidget(EncoderCodec::PART_ADVANCE,*m_option);

	if (common) {
	    if (m_lastExtraConfigWidget) {
		ui->extraConfigLayout->removeWidget(m_lastExtraConfigWidget);
		m_lastExtraConfigWidget->hide();
		m_lastExtraConfigWidget->deleteLater();
	    }

	    ui->extraConfigLayout->insertWidget(1,common,1);

	    m_lastExtraConfigWidget = common;
	    setExtraConfigDisplay(true);

	    ui->btnAdvanceOption->setEnabled(advance!=NULL);

	    if (advance) {
		ui->btnAdvanceOption->disconnect();
		advance->setWindowModality(Qt::ApplicationModal);
		connect(ui->btnAdvanceOption,SIGNAL(clicked()),
			advance,SLOT(show()));
	    }
	}else{
	    setExtraConfigDisplay(false);
	}
    }

    ui->labelHelpVCodec->setText(help);
}

void SettingWidget::on_cbAudioCodecs_currentIndexChanged(int index)
{
    QVariant data = ui->cbAudioCodecs->itemData(index,Qt::UserRole);
    EncoderCodec *codec;
    QString help;
    if (data.isValid()) {
	codec = data.value<EncoderCodec *>();
	help = codec->introduction();
    }


    ui->labelAudioCodecHelp->setText(help);
}

void SettingWidget::on_btnCustom_clicked()
{
    int ret = m_videoQualitySetting->exec();
    ret = 0;
}

void SettingWidget::on_rbCustomQ_clicked(bool checked)
{
    ui->btnCustom->setEnabled(checked);
}

void SettingWidget::setExtraConfigDisplay(bool d) {
    QWidget *w = NULL;
    int i=0;
    while( (w = ui->extraConfigLayout->itemAt(i++)->widget()) != NULL ) {
	w->setHidden(!d);
    }
}
